package com.rongkang.interview.juc.multithreading;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @author: 荣康
 * @date: 2021/12/11
 */
class MyThread implements Callable {
    @Override
    public Object call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"come in callable");
        TimeUnit.SECONDS.sleep(2);
        return 1024;

    }
}

public class CallableDemo {
    public static void main(String[] args) throws Exception {
        FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread());
        FutureTask<Integer> futureTask2 = new FutureTask<Integer>(new MyThread());
        new Thread(futureTask,"AA").start();
        new Thread(futureTask2,"BB").start();

        System.out.println(Thread.currentThread().getName()+"*******88");
        Integer result1 = 100;
        //获取call方法的返回值，注意这个get一般要写在最后面，，如果没有计算完就去强求，会造成线程的堵塞
//        while (futureTask.isDone()){
//        }
        Integer result2 = futureTask.get();
        System.out.println("******* result \t" + (result1 + result2));
    }
}
